var baseFile = "ui";
var base = "";
console.log(function () {});
var type = window.sessionStorage.getItem('type');
if (type == 2){
    $(".secondary").hide();
}
//校验手机号
function checkTel(tel) {
    var isPhone = /^([0-9]{3,4}-)?[0-9]{7,8}$/;
    //座机验证
    var isMob = /^((\+?86)|(\(\+86\)))?(13[012356789][0-9]{8}|15[012356789][0-9]{8}|18[02356789][0-9]{8}|147[0-9]{8}|1349[0-9]{7})$/;
    if (isMob.test(tel) || isPhone.test(tel)) {
        return true;
    }
    else {
        return false;
    }
}
//校验邮箱
function checkEmail(email) {
    var isEmail = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/;

    if (isEmail.test(email)) {
        return true;
    }
    else {
        return false;
    }
}
//时间戳转换为时间
//转成年月日
//转时间
function timestampToTime(timestamp) {

    var time = new Date(timestamp).toLocaleDateString();
    var timeArr = time.split("/");

    if(timeArr[1].length === 1){
        timeArr[1] = "0" + timeArr[1];
    }
    if(timeArr[2].length === 1){
        timeArr[2] = "0" + timeArr[2];
    }

    var timeStr = "";
    timeStr = timeArr.reduce(function(a ,b){
        return a + "-" + b;
    });

    return timeStr;
}
function timestampToYear(timestamp) {

    var time = new Date(timestamp).toLocaleDateString();
    var timeArr = time.split("/");

    if(timeArr[1].length === 1){
        timeArr[1] = "0" + timeArr[1];
    }
    if(timeArr[2].length === 1){
        timeArr[2] = "0" + timeArr[2];
    }

    var timeStr = "";
    timeStr = timeArr.reduce(function(a ,b){
        return a + "-" + b;
    });

    return timeStr;
}
//带时间
function add0(m) {
    return m < 10 ? '0' + m : m
}
//时间戳转成带年月日时分秒的时间
function changeToHour(shijianchuo) {
//shijianchuo是整数，否则要parseInt转换
    var time = new Date(shijianchuo);
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    var h = time.getHours();
    var mm = time.getMinutes();
    var s = time.getSeconds();
    return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
}


function getAjaxJson(url, data, fn) {
    $.ajax({
        url: base + url + oid,
        type: 'post',
        dataType: 'json',
        data: data,
        contentType: 'application/json',
        success: function (msg) {
            fn(msg);
        },
        error: function (err) {
            console.log(err);
        }

    })
}
//提交修改密码
function upPassword() {
    var psdInfo = {};
    psdInfo.laId = id;
    if ($('#oldpwd').val()== ''){
        swal(
            '请输入原密码',
            'error'
        );
        return;
    }
    else{
        psdInfo.adminPassword = $('#oldpwd').val();
    }
    if ($('#newpwd').val().length>15){
        swal(
            '修改失败！',
            '密码不得多余15个字符',
            'error'
        );
        return;
    }else if ($('#newpwd').val()==''){
        swal(
            '新密码不得为空！',
            'error'
        );
        return;
    }
    else{
        psdInfo.newPsw = $('#newpwd').val();
    }
    console.log(psdInfo);
    getAjax('/auth/psw/reset',psdInfo,function (msg) {
        if(msg.resultCode == 0){
            swal(
                '修改密码成功！',
                '',
                'success'
            );
            setTimeout(window.location.href='/login',1000);
        }
        else{
            console.log(msg.resultCode);
            swal(
                '原密码不正确！',
                '',
                'error'
            );                }
    })
}
function upPassword1() {
    var psdInfo = {};
    psdInfo.laId = id;
    if ($('#oldpwd').val()== ''){
        swal(
            '请输入原密码',
            'error'
        );
        return;
    }
    else{
        psdInfo.adminPassword = $('#oldpwd').val();
    }
    if ($('#newpwd').val().length>15){
        swal(
            '修改失败！',
            '密码不得多余15个字符',
            'error'
        );
        return;
    }else if ($('#newpwd').val()==''){
        swal(
            '新密码不得为空！',
            'error'
        );
        return;
    }
    else{
        psdInfo.newPsw = $('#newpwd').val();
    }
    console.log(psdInfo);
    getAjax('/auth/psw/reset',psdInfo,function (msg) {
        if(msg.resultCode == 0){
            swal(
                '修改密码成功！',
                '',
                'success'
            );
            setTimeout(window.location.href='/login',1000);
        }
        else{
            console.log(msg.resultCode);
            swal(
                '原密码不正确！',
                '',
                'error'
            );                }
    })
}
//ajax封装
function getAjax(url,data,fn) {
    $.ajax({
        url:  base + url,
        type: 'post',
        dataType: 'json',
        data:data,
        success: function(msg){
            fn(msg);
        },
        error: function(err){
            console.log(err);
        }

    })
}

//显示用户名
var realName = window.sessionStorage.getItem('username');
if (realName){
    $(".username").html("用户名："+realName);
}
else {
    $(".username").html("");
}
